package it.unibo.iss.domotica;

import it.unibo.iss.contact.domoticaContact.SysDomotichUsrcmd;
import it.unibo.iss.contact.domoticaContact.UsrcmdContact;
import it.unibo.iss.domotica.converter.DomoticaJsonConverter;
import it.unibo.iss.domotica.edDomain.Elettrodomestico;
import it.unibo.iss.domotica.edDomain.StatoED;

import java.util.ArrayList;

import android.util.Log;

public class Usrcmd {
	private ArrayList<Elettrodomestico> elettrodomestici = new ArrayList<Elettrodomestico>();
	private ArrayList<Elettrodomestico> listEdSuperflui;
	private UsrcmdContact usrcmdContact;
	private SysDomotichUsrcmd system;
	private DomoticaJsonConverter converter;
	private static final String TAGFORLOG = "Usrcmd";
	private boolean error;
	private String errorMessage;
	
	public Usrcmd() {
		// Call constructor with support initialization
		this(true);
	}
	
	/*
	 * Constructor used for testing operations. 
	 * Used to initialize (doSupportInit=true) or not (doSupportInit=false) 
	 * the support 
	 */
	public Usrcmd(boolean doSupportInit) {
		if(doSupportInit) {
			system = new SysDomotichUsrcmd();
			usrcmdContact = system.getUsrcmdContact();
		}
		else {
			try {
				usrcmdContact = new UsrcmdContact();
				usrcmdContact.setName("usrcmdContact");
				usrcmdContact.setEnv(null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		converter = new DomoticaJsonConverter();
		error = false;
	}
	
	public boolean isError() {
		return error;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public ArrayList<Elettrodomestico> getElettrodomestici() {
		return elettrodomestici;
	}

	public UsrcmdContact getUsrcmdContact() {
		return usrcmdContact;
	}

	public ArrayList<Elettrodomestico> updateListEd() {
		error = false;
		
		try {
			String listEd = usrcmdContact.demandListED();
			elettrodomestici = converter.stringToElettrodomesticoArrayList(listEd);
			return elettrodomestici;
		} catch (Exception e) {
			e.printStackTrace();
			error = true;
			errorMessage = "Unable to update household appliances list: " + e;
		}
		
		return null;
	}
	
	public ArrayList<Elettrodomestico> powerOn(String edName) {
		error = false;
		
		Elettrodomestico ed = CommonUtilities.getElettrodomestico(elettrodomestici, edName);
		
		if(ed != null) {
			try {
				int res = usrcmdContact.demandPowerOn(edName);
				
				if(res == UsrcmdContact.SUCCESS) {
					ed.setStato(StatoED.CONDITIONED);
				}
				else if(res == UsrcmdContact.CONSUMPTION_HIGH){
					String listEdSuperfluiString = usrcmdContact.waitForListEdSuperflui();
					listEdSuperflui = converter.stringToElettrodomesticoArrayList(listEdSuperfluiString);
					
					return listEdSuperflui;
				}
				else {
					error = true;
					errorMessage = "Unable to Power On " + edName;
				}
			} catch (Exception e) {
				e.printStackTrace();
				error = true;
				errorMessage = "Unable to Power On " + edName + ": " + e;
			}
		}
		
		return null;
	}
	
	public boolean powerOff(String edName) {
		error = false;
		
		for(Elettrodomestico ed : elettrodomestici) {
			if(ed.getNome().equals(edName)) {
				try {
					if(usrcmdContact.demandPowerOff(edName)) {
						ed.setStato(StatoED.OFF);
						return true;
					}
					else {
						error = true;
						errorMessage = "Unable to power off " + edName;
					}
				} catch (Exception e) {
					e.printStackTrace();
					error = true;
					errorMessage = "Error while powering off " + edName + ": " + e;
				}
				break;
			}
		}
		
		return false;
	}
	
	public boolean forwardAbortPowerOffSuperflui() {
		error = false;
		
		try {
			usrcmdContact.forwardAbortPowerOffSuperflui();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			error = true;
			errorMessage = "Unable to forward abort command: " + e;
		}
		
		return false;
	}
	
	public String waitForListEdSuperflui() throws Exception {
		return usrcmdContact.waitForListEdSuperflui();
	} 
}
